package com.avp;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.os.Handler;
import android.widget.*;

public class Activity2 extends Activity {

	public AlertDialog dialog;
	public RingerConnection connect;
	public RingerServer rserver;
	public RingerClient rclient;
	
	public Handler h;

	public void onPause() {

		hangup();
		if (dialog != null) {
			connect.setResponse("reject");
			dialog.dismiss();
		}
		Intent intent = new Intent();
		setResult(RESULT_OK, intent);
		// DirectoryClient dClient = new DirectoryClient;
		// DirectoryClient.logout();

		DirectoryClient.disconnect();

		finish();
		super.onPause();
	}

	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {

		DirectoryClient.pollUsers();
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main2);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		RingerServer rserv = new RingerServer(this);
		new Thread(rserv).start();
		rserver = rserv;
		ArrayList<String> userlist = DirectoryClient._userList;

		Button next = (Button) findViewById(R.id.button2);
		Button hangup = (Button) findViewById(R.id.button3);

		hangup.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				hangup();

			}

		});
		next.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				// System.err.println("Error");
				Intent intent = new Intent();
				setResult(RESULT_OK, intent);
				// DirectoryClient dClient = new DirectoryClient;
				// DirectoryClient.logout();

				DirectoryClient.disconnect();

				finish();
			}
		});

		// setListAdapter(new ArrayAdapter<String>(this, R.layout.main,
		// userlist));

		ListView lv = (ListView) findViewById(R.id.userlist);

		ArrayList<String> values = DirectoryClient._userList;

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, android.R.id.text1, values);

		lv.setAdapter(adapter);

		final ArrayAdapter<String> a = (ArrayAdapter<String>) adapter;
		Handler handle = new Handler() {
			@Override
			public void handleMessage(Message m) {

				a.notifyDataSetChanged();

			}
		};

		DirectoryClient.adapter = handle;
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				hangup();
				// When clicked, show a toast with the TextView text
				Toast.makeText(getApplicationContext(),
						((TextView) view).getText(), Toast.LENGTH_SHORT).show();
				rclient = new RingerClient("someone", 9999);
				String callingIP = DirectoryClient.getIP(((TextView) view)
						.getText().toString());
				rclient.call(callingIP, 9999);

			}
		});

		h = new Handler() {
			public void handleMessage(Message msg) {

				incomingCall();
				getWindow().getDecorView().findViewById(android.R.id.content)
						.postInvalidate();
			}
		};

	}

	public void hangup() {
		if (rserver!=null)
		rserver.close();
		if (rclient!=null)
		rclient.closeAll();
	}

	public void incomingCall() {
		Log.d("ring", "trying to make dialogue");
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		dialog = alertDialog;
		alertDialog.setTitle("Incoming Call.");
		alertDialog.setMessage("Do you want to connect?");
		connect = rserver.getConn();

		alertDialog.setButton2("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				connect.setResponse("accept");
				dialog.dismiss();

			}
		});
		alertDialog.setButton3("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				connect.setResponse("reject");
				dialog.dismiss();

			}
		});
		alertDialog.show();

	}

}
